package com.atguigu.order.service.impl;


import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.atguigu.Order.Order;
import com.atguigu.Product.Product;
import com.atguigu.order.feigin.ProductFeignClient;
import com.atguigu.order.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;

import java.math.BigDecimal;
import java.util.List;


@Service
@Slf4j
public class OrderServiceImpl implements OrderService {

    @Autowired
    DiscoveryClient discoveryClient;

    @Autowired
    RestTemplate restTemplate;

    @Autowired
    LoadBalancerClient loadBalancerClient;

    @Autowired
    ProductFeignClient productFeignClient;

    @SentinelResource(value = "createOrder",blockHandler = "createOrderFallback")
    @Override
    public Order createOrder(Long productId, Long userId) {

//        Product product = getProduct(productId);
//        Product product = getProductWithLoadBalance(productId);
//        Product product = getProductWithLoadBalanceAnno(productId);

        // feign客户端
        Product product = productFeignClient.getProductById(productId);
        Order order = new Order();
        order.setId(1L);

        // 总金额
        order.setTotalAmount(product.getPrice().multiply(new BigDecimal(product.getNum())));
        order.setUserId(userId);
        order.setNickName("kangkang");
        order.setAddress("尚硅谷");
        //远程查询商品列表
        order.setProducts(List.of(product));


        return order;
    }


    public Order createOrderFallback(Long productId, Long userId, BlockException e) {
        Order order = new Order();
        order.setId(0L);
        order.setTotalAmount(new BigDecimal("0"));
        order.setUserId(userId);
        order.setNickName("未知用户");
        order.setAddress("异常信息" + e.getClass());
        return order;
    }


    
    /**
     * 不使用LoadBalancerClient
     * @param productId
     * @return
     */
    private Product getProduct(Long productId){
        List<ServiceInstance> instances = discoveryClient.getInstances("service-product");
        ServiceInstance serviceInstance = instances.get(0);
        String url = "http://"+serviceInstance.getHost()+":"+serviceInstance.getPort()+"/product/getProduct/"+productId;

        log.info("url:{}",url);
        Product product = restTemplate.getForObject(url, Product.class);
        return product;
    }

    /**
     * 使用LoadBalancerClient
     * @param productId
     * @return
     */
    private Product getProductWithLoadBalance(Long productId){

        ServiceInstance choose = loadBalancerClient.choose("service-product");
        String url = "http://"+choose.getHost()+":"+choose.getPort()+"/product/getProduct/"+productId;

        log.info("url:{}",url);
        Product product = restTemplate.getForObject(url, Product.class);
        return product;
    }

    /**
     *  使用@LoadBalanced
     * @param productId
     * @return
     */
    private Product getProductWithLoadBalanceAnno(Long productId){
        String url = "http://service-product/product/getProduct/"+productId;

        log.info("url:{}",url);
        Product product = restTemplate.getForObject(url, Product.class);
        return product;
    }
}
